package cn.gzzhome.utils
/**
 * 自带变更属性的task
 */
class ScmTask extends Task {

    ScmHelper scmHelper

    //当前是否有变动
    Boolean isChange

    //过去是否有变动
    Boolean hasChange

    /**
     * 判断当前构建是否有变更
     */
    def isChange() {
        if (isChange == null) {
            def result = (scmHelper.lastSuccessBuild == null)
            if (!result && paths) {
                result = paths.any {
                    scmHelper.isChange(it, excludePaths)
                }
            }
            if (!result && dependOnList) {
                result = dependOnList.any {
                    if (ScmTask.class.isAssignableFrom(it.getClass())) {
                        return ((ScmTask) it).isChange()
                    } else {
                        return false
                    }
                }
            }
            isChange = result
        }
        return isChange
    }

    /**
     * 过去是否有改动
     * @return
     */
    def hasChange() {
        if (hasChange == null) {
            def result = (scmHelper.lastSuccessBuild == null)
            if (!result && paths) {
                result = paths.any {
                    scmHelper.hasChange(it, excludePaths)
                }
            }
            if (!result && dependOnList) {
                result = dependOnList.any {
                    if (ScmTask.class.isAssignableFrom(it.getClass())) {
                        return ((ScmTask) it).hasChange()
                    } else {
                        return false
                    }
                }
            }
            hasChange = result
        }
        return hasChange
    }

    @Override
    def needProcess() {
        return isChange() || hasChange()
    }
}
